package com.loadmore.common.base;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.loadmore.common.image.ImageLoader;

import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

public class RVBaseHolder<T extends View> extends RecyclerView.ViewHolder {
    SparseArray<View> views;
    public RVBaseHolder(View itemView) {
        super(itemView);
        views = new SparseArray<>();
    }

    public T retrieve(int id) {
        View view = views.get(id);
        if (view == null) {
            view = itemView.findViewById(id);
        }
        views.put(id,view);
        return (T) view;
    }

    public TextView getText(int id) {
        return (TextView) retrieve(id);
    }

    public CheckBox getCheckBox(int id) {
        return (CheckBox) retrieve(id);
    }

    public ImageView getImageView(int id) {
        return (ImageView) retrieve(id);
    }


    public RVBaseHolder setText(int id,String text) {
        TextView textView = (TextView) retrieve(id);
        textView.setText(text);
        return this;
    }
    public RVBaseHolder setButton(int id,String text) {
        AppCompatButton button = (AppCompatButton) retrieve(id);
        button.setText(text);
        return this;
    }
    public RVBaseHolder setImage(Context context,int id, String url) {
        ImageView imageView = (ImageView) retrieve(id);
        if (imageView != null && !TextUtils.isEmpty(url)) {
            ImageLoader.INSTANCE.load(context,url,imageView);
        }
        return this;
    }

    public RVBaseHolder setCheck(int id,boolean status) {
        CheckBox checkBox = (CheckBox) retrieve(id);
        checkBox.setChecked(status);
        return this;
    }

}